﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LinqKit;

using CMS.DataLibrary;
using CMS.Core.Types;

namespace CMS.Core
{
    public class UserManager : BaseManager<User>
    {

        /// <summary>
        /// 判断用户是否存在 
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns></returns>
        public bool HasUserName(string userName)
        {
            return base.Repository.IsContains(a => a.Username.ToLower() == userName.ToLower());
        }

        /// <summary>
        /// 判断Email是否存在
        /// </summary>
        /// <param name="email">Email</param>
        /// <returns></returns>
        public bool HasEmail(string email)
        {
            return base.Repository.IsContains(a => a.Email.ToLower() == email.ToLower());
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="entity">用户实体</param>
        /// <returns>0-失败, 1-成功, 2-用户名存在, 3-Emai存在</returns>
        public override Response Add(User entity)
        {
            Response resp = new Response();

            if(!string.IsNullOrEmpty(entity.Username) && HasUserName(entity.Username))
            {
                resp.Code = 2;
                resp.Message = "用户名已经存在";
            }
            else if(!string.IsNullOrEmpty(entity.Email) && HasEmail(entity.Email))
            {
                resp.Code = 3;
                resp.Message = "Emai已经存在";
            }
            else
            {
                resp = base.Add(entity);
            }

            return resp;
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="pagingUser">分页信息</param>
        /// <param name="roleID">角色ID</param>
        /// <param name="userName">用户名</param>
        /// <param name="name">名字</param>
        /// <param name="sex">性别</param>
        /// <param name="email">邮箱</param>
        /// <param name="order">排序方式</param>
        /// <returns>排序【null（默认）-ID降序，0-ID升序，1-ID降序，2-注册时间降序，3-注册时间升序，4-最后登录时间升序，5-最后登录时间降序】</returns>
        public Paging<User> FindListPage(Paging<User> pagingUser, int? roleID, string userName, string name, int? sex, string email, int? order)
        {
            var where = PredicateBuilder.True<User>();

            if (roleID != null && roleID > 0)
                where = where.And(u => u.RoleID == roleID);
            if (!string.IsNullOrEmpty(userName))
                where = where.And(u => u.Username.Contains(userName));
            if (!string.IsNullOrEmpty(name))
                where = where.And(u => u.Name.Contains(name));
            if (sex != null && sex >= 0 && sex <= 2)
                where = where.And(u => u.Sex == sex);
            if (!string.IsNullOrEmpty(email))
                where = where.And(u => u.Email.Contains(email));
            
            //排序
            OrderParam _orderParam;
            switch(order)
            {
                case 0://ID升序
                    _orderParam = new OrderParam() { PropertyName = "UserID", Method = OrderMethod.ASC };
                    break;
                case 1://ID降序
                    _orderParam = new OrderParam() { PropertyName = "UserID", Method = OrderMethod.DESC };
                    break;
                case 2://注册时间降序
                    _orderParam = new OrderParam() { PropertyName = "RegTime", Method = OrderMethod.ASC };
                    break;
                case 3://注册时间升序
                    _orderParam = new OrderParam() { PropertyName = "RegTime", Method = OrderMethod.DESC };
                    break;
                case 4://最后登录时间升序
                    _orderParam = new OrderParam() { PropertyName = "LastLoginTime", Method = OrderMethod.ASC };
                    break;
                case 5://最后登录时间降序
                    _orderParam = new OrderParam() { PropertyName = "LastLoginTime", Method = OrderMethod.DESC };
                    break;
                default://ID降序
                    _orderParam = new OrderParam() { PropertyName = "UserID", Method = OrderMethod.DESC };
                    break;
            }

            pagingUser.Items = Repository.FindPageList(pagingUser.PageSize, pagingUser.PageIndex, out pagingUser.TotalItems, where.Expand(), _orderParam).ToList();

            return pagingUser;
        }

    }
}
